/**
 * Created by Wing on 2017/5/25.
 */
function two_char(n) {
    return n >= 10 ? n : "0" + n;
}
var sec = 0;
var interval;
var time_fun = function () {
    if (interval != undefined) {
        clearInterval(interval);
    }
    interval = setInterval(function () {
        sec++;
        var date = new Date(0, 0);
        date.setSeconds(sec);
        var m = date.getMinutes(),
            s = date.getSeconds();
        document.getElementById("mytime").innerText = two_char(m) + ":" + two_char(s);
    }, 1000);
}

var newCards = [];
var oldCards = [];
var oldCard = [];
var cardStudyArray = []; //[DeckNoteNoteTypeCardStyleCard]
var studyScheme = {
    "studySchemeId": "",
    "userId":"",
    "studySchemeName":"",
    "ignoreTimeMoreThan":"",
    "studySequence":"",
    "newCardCount":20,
    "maxReviewCount":100
};
var defaultCard = [];
defaultCard.length = 5; //通过学习方案的得到的默认的要学习的卡片的总数。
var getCardToStudy = function (studyListId) {
    $.getJSON(
        "/memorypalace/study/studybystudylist.action?studyListId=" + studyListId,
        function (cardStudy) {
            // cardStudy{"newCount":"","reviewCount":"","cards":[]}
            cardStudyArray = cardStudy["cards"];
            if(cardStudyArray.length == 0){
                window.location.href = "/memorypalace/studyEnd.html";
            }
            var newCount = cardStudy["newCardStudyCount"];
            var reviewCount = cardStudy["reviewCardStudyCount"];
            $("#newCount").html(newCount);
            $("#reviewCount").html(reviewCount);
            if(newCount>0){
                $("#newCount").addClass("text");
            }else{
                $("#newCount").removeClass("text");
                $("#reviewCount").addClass("text");
            }
            // 将获取到的分组，并限制新卡片的个数
            currentIndex = 0;
            changeCardShow();
        }
    );
};
var studyListId;
$(function () {
    time_fun();
    studyListId = $("#studyListId").val();
    getCardToStudy(studyListId);

});


//拿20新的，100旧的
/* var data = [ {
 cardId : cardStudyArray[0].card.cardId,
 cardNextStudyTime : cardStudyArray[0].card.cardNextStudyTime
 } ] */


// 当前应该显示的卡片的索引
var currentIndex = 0;
var isFront = true;

// 更换正反面的显示
var changeCardShow = function () {
    //	var currentCard = cardStudyArray[currentIndex];
    if(currentIndex == undefined){
        currentIndex = 0;
    }
    var currentCard = cardStudyArray[currentIndex];

    var html;
    if (isFront) {
        html = currentCard.cardStyle.frontHTML;
    } else {
        html = currentCard.cardStyle.backHTML;
        html = html.replace("{{FrontSide}}",currentCard.cardStyle.frontHTML);
    }


    var areaContentsJSON = JSON.parse(currentCard.note.areaContents);
    var areaNamesJSON = JSON.parse(currentCard.note.areaNames);
    $.each(areaNamesJSON, function (i, areaName) { //正面
        html = html.replace("{{" + areaName + "}}", areaContentsJSON[i]);
    });

    $(".card").html(html);

    $("#cardCss").append(currentCard.cardStyle.css);
}
// 下方按钮的点击
var btnclick = function (e) {
    if (isFront) {
        isFront = false;
        changeCardShow();
        $('#_zmian').hide();
        $('#_fmian').show();
    } else {
        currentIndex++;
        $.ajax({
            type: "post",
            url: "/memorypalace/study/insertcsr.action",
            data: {
                costSeconds: sec,
                answerButton: e.value,
                //cardId : cardStudyArray[currentIndex - 1].card.cardId
                cardId: cardStudyArray[currentIndex - 1].card.cardId,
                studyListId:studyListId
            },
            success: function (studyIntervalJSON) {
                getCardToStudy(studyListId);
            }
        });

        document.getElementById("mytime").innerText = '00:00';

        sec = 0;
        time_fun();

        //if ((currentIndex == cardStudyArray.length) && (!isFront)) {
        if ((currentIndex == defaultCard.length) && (!isFront)) {
            //  alert("gongxi");
            //	window.location.href = "/memorypalace/study/getsl.action";
            window.location.href = "/memorypalace/studyEnd.html";
        } else {
            isFront = true;
            changeCardShow();
            $('#_zmian').show();
            $('#_fmian').hide();
            console.log(sec);
        }
    }

};